"use strict";
 
const moment = require('moment');   
const jwt = require('jsonwebtoken');
module.exports = app => {
  return class SysteamController extends app.Controller {
    async login() {
      
      const { name="",pwd="" } = Object.assign({},this.ctx.query,this.ctx.request.body)
      console.log(name,pwd)
      if(!name.trim() || !pwd.trim()){
        this.ctx.body = { code:-1,  };
        return 
      }
      const create_time = moment().format('YYYY-MM-DD hh:mm:ss')
      const update_time =  create_time;
      let data = await this.app.mysql.select('user', { where:{ name,pwd }});
      var token = '';
      if(data.length){
        await this.app.mysql.update('user',{ update_time }, { where:{ name,pwd }});
        token = jwt.sign({ userId: data[0].id,name }, app.config.jwt.secret, { expiresIn: '1d' });
      }else{
        data = await app.mysql.insert('user', {name,pwd,thumbs:0,update_time, create_time } );
        token = jwt.sign({ userId: data.insertId,name }, app.config.jwt.secret, { expiresIn: '1d' });
      }
      this.ctx.body = { code:0, data,token};
    }
    async types() {
      const data = await this.app.mysql.select('type');
      this.ctx.body = { code:0,data };
    }
    async type() {
      console.log(this.ctx.query,this.ctx.request.body)
      const {row,token} = Object.assign({},this.ctx.query,this.ctx.request.body)
      if(!row.name.trim() || !row.update_time.trim()||!row.create_time.trim()){
        this.ctx.body = { code:-1,  };
        return 
      }
      let data = await this.app.mysql.select('type', { where:{ name:row.name }});
      if(!data.length){
        const update_time = moment().format('YYYY-MM-DD hh:mm:ss')
        await this.app.mysql.update('user',{ name:row.name,update_time }, { where:{ id:data[0].id }});
        data = await app.mysql.insert('type', row);
      } 
      this.ctx.body = { code:0,data };
    }
    
    async components() {
      const { tid, uid,page = 1, limit = 10,info = ''} = Object.assign({},this.ctx.query,this.ctx.request.body)
      

      const offset = (page - 1) * limit; 
      const where = {}
      let more = ' and  c.uid = u.id'
      if(tid){
        where.tid = tid;
        more += ' and c.tid = ' + tid
      }
      if(uid) {
        where.uid = uid;
        more += ' and  c.uid = ' + uid 
      }
      if(info){
        where.info =[ 'like', `%${info}%` ];
        more += ` and  c.info like %${info}%`
      }
      //const data = await this.app.mysql.select('component', { where,limit,offset,});
      const data = await this.app.mysql.query(`select c.id,c.path,c.info,c.thumbs,u.id,u.name from component c , user u where 1=1 ${more} limit ${offset},${limit}`)
      this.ctx.body = { code:0,data };
    }

    /* 简易实现 不检测参数 */
    async component() {
      const create_time = moment().format('YYYY-MM-DD hh:mm:ss')
      const update_time =  create_time;
      const {row,token} = Object.assign({},this.ctx.query,this.ctx.request.body)
      if(!token.trim()){
        this.ctx.body = { code:-1,  };
        return 
      }
     
      const decoded = jwt.verify(token, this.ctx.app.config.jwt.secret);
      console.log(decoded)
      const data = await app.mysql.insert('component', Object.assign(row,{create_time,update_time}));
      this.ctx.body = { code:0,data };
    }
    
    async path() {
      const { path,token } = Object.assign({},this.ctx.query,this.ctx.request.body)
      const data = await this.app.mysql.select('component', { where:{ path } });
      this.ctx.body = { code:0,data };
    }
  }
} 